package pluginxmongo

import (
	"gitee.com/douhun/easy-ngo/clients/xjwt"
)

var xjwts map[string]*xjwt.JWT

func set(key string, jwt *xjwt.JWT) {
	if xjwts == nil {
		xjwts = make(map[string]*xjwt.JWT, 1)
	}
	xjwts[key] = jwt
}

func GetJWTByKey(key string) (xjwt *xjwt.JWT) {
	var ok bool
	jwt, ok := xjwts[key]
	if !ok {
		return nil
	}
	return jwt
}

func GetJWT() (xjwt *xjwt.JWT) {
	return GetJWTByKey("default")
}

func GenTokenByKey(key string, username string) (string, error) {
	config := GetJWTByKey(key)
	return xjwt.CreateToken(config.Config, username)
}

func ParseTokenByKey(key string, tokenString string) (*xjwt.BaseClaims, error) {
	config := GetJWTByKey(key)
	return xjwt.ParseToken(config.Config, tokenString)
}

func GenToken(username string) (string, error) {
	config := GetJWT()
	return xjwt.CreateToken(config.Config, username)
}

func ParseToken(tokenString string) (*xjwt.BaseClaims, error) {
	config := GetJWT()
	return xjwt.ParseToken(config.Config, tokenString)
}
